<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>${systemName}</title>
    <meta name="description" content="${systemName}">
    <meta name="author" content="stylefeng">

    <link rel="stylesheet" href="${ctxPath}/assets/common/layui/css/layui.css"/>
    <link href="${ctxPath}/assets/expand/plugins/ztree/zTreeStyle.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        #school{
            display: none;
        }
    </style>
</head>

<body>
<div id="deptForm" class="layui-fluid">
    <div class="layui-row" style="margin-top:15px;background: #f2f7f8;padding: 20px;">
        <ul id="zTree" class="ztree"></ul>
    </div>
    <form id="userForm" lay-filter="userForm" class="layui-form model-form" style="padding-top: 20px;">
        <div class="layui-form-item" id="school">
            <label class="layui-form-label">学校<span style="color: red;">*</span></label>
            <div class="layui-input-block">
                <select id="schoolId" name="schoolId" lay-verify="">
                    <option value="">请选择学校</option>
                </select>
            </div>
        </div>
    </form>
    <div class="layui-row" style="background: #CFD4D5;padding: 10px;">
        <div style="text-align: center;">
            <button class="layui-btn layui-btn-sm" id="saveButton">保存</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </div>
</div>

@/* 加入contextPath属性和session超时的配置 */
<script type="text/javascript">
    var Feng = {
        ctxPath: "",
        addCtx: function (ctx) {
            if (this.ctxPath === "") {
                this.ctxPath = ctx;
            }
        }
    };
    Feng.addCtx("${ctxPath}");
</script>
<script type="text/javascript" src="${ctxPath}/assets/common/layui/layui.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js"></script>

<!--其他插件js-->
<script type="text/javascript" src="${ctxPath}/assets/expand/plugins/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/expand/plugins/ztree/jquery.ztree.all.min.js"></script>

<script type="text/javascript">

    layui.use(['form','ztree','ax'], function () {
        var $ZTree = layui.ztree;
        var $ax = layui.ax;
        var $ = layui.$;
        var form = layui.form;

        var ZTreeDlg = {
            index: parent.layer.getFrameIndex(window.name)
        };

        $("#saveButton").bind("click", function () {
            var treeObj = $.fn.zTree.getZTreeObj("zTree");
            var nodes = treeObj.getNodes();
            for(var i=0; i< nodes.length; i++){
                if(nodes[i].checked && "校区管理员"===nodes[i].name){
                    if($("#schoolId").val()==null || $("#schoolId").val()==""){
                        Feng.error("校区管理员请选择学校")
                        return
                    }
                }
            }

            var ids = Feng.zTreeCheckedNodes("zTree");
            var ajax = new $ax(Feng.ctxPath + "/mgr/setRole", function (data) {
                Feng.success("分配角色成功!");
                parent.layer.close(ZTreeDlg.index);
            }, function (data) {
                Feng.error("分配角色失败!" + data.responseJSON.message + "!");
            });
            ajax.set("roleIds", ids);
            ajax.set("userId", "${userId}");
            ajax.set("schoolId",$("#schoolId").val())
            ajax.start();
        });

        var setting = {
            check: {
                enable: true,
                chkboxType: {
                    "Y": "",
                    "N": ""
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback:{
                onCheck:zTreeOnCheck
            }
        };

        var ztree = new $ZTree("zTree", "/role/roleTreeListByUserId/${userId}");
        ztree.setSettings(setting);
        ztree.init();

        var treeObj = $.fn.zTree.getZTreeObj("zTree");
        var nodes = treeObj.getNodes();
        for(var i=0; i< nodes.length; i++){
            if(nodes[i].checked && "校区管理员"===nodes[i].name){
                $("#school").show()
                //查询所有学校
                var ajax = new $ax(Feng.ctxPath + "/school/selectSchool", function (data) {
                    $("#schoolId").append(data)
                })
                ajax.start();
                //重新渲染select
                form.render('select');
                //获取用户信息
                var ajax = new $ax(Feng.ctxPath + "/mgr/getUserInfo?userId=${userId}");
                var result = ajax.start();
                form.val('userForm', result.data);
            }
        }


        function zTreeOnCheck(event,treeId,treeNode){
            if("校区管理员"===treeNode.name){
                if(treeNode.checked){
                    //校区管理员需要设置学校id,才能来管理学校
                    $("#school").show()
                    //查询所有学校
                    var ajax = new $ax(Feng.ctxPath + "/school/selectSchool", function (data) {
                        $("#schoolId").append(data)
                    })
                    ajax.start();
                    //重新渲染select
                    form.render('select');
                    //获取用户信息
                    var ajax = new $ax(Feng.ctxPath + "/mgr/getUserInfo?userId=${userId}");
                    var result = ajax.start();
                    form.val('userForm', result.data);
                }else{
                    $("#schoolId option").remove()
                    $("#school").hide()
                }


            }
        }
    });




</script>

</body>

</html>
